#include "mymultisignalchart.h"
#include <QDateTime>
#include "chart.h"
#include "chartview.h"
#include <QApplication>
#include <QDateTimeAxis>
#include <QLineSeries>
#include <QRandomGenerator>
#include <QChart>
#include <QChartView>
#include <QMainWindow>
#include <cmath>
#include <QtMath>
#include <QValueAxis>
#include <QValueAxis>
#include <QDate>
#include<QDebug>
#include <QTimer>
#include <QEventLoop>
#include<QVBoxLayout>
#include"TimerWorker.h"
#include"qboxlayout.h"
void delay(int milliseconds) {
    QEventLoop loop;
    QTimer::singleShot(milliseconds, &loop, &QEventLoop::quit);
    loop.exec();
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    /////////////////////////[]/////////////////////////////////////////////////
    /// \brief series1
    ///
    auto series1 = new QLineSeries;
    QColor color1(0, 0, 0);
    series1->setName("signal1");
    series1->setColor(color1);
    series1->setUseOpenGL(true);
    auto chart1 = new Chart;
    chart1->addSeries(series1);
    chart1->setTitle("SIGNAL1");
    chart1->setAnimationOptions(QChart::SeriesAnimations);
    chart1->setTheme(chart1->ChartThemeDark);
    auto axisX1 = new QValueAxis();
    axisX1->setTickCount(11);
    axisX1->setMin(0);
    axisX1->setMax(1440);
    axisX1->setTitleText("time1");
    //axisX1->hide();
    axisX1->setLineVisible(true);
    auto axisY1 = new QValueAxis();
    axisY1->setMin(0);
    axisY1->setMax(10);
    axisY1->setTickCount(11);
    axisY1->setTitleText("signals1");
    //axisY1->hide();
    chart1->setAxisX(axisX1, series1);
    chart1->setAxisY(axisY1, series1);
    auto chartView1 = new ChartView(chart1);
    chartView1->setRenderHint(QPainter::Antialiasing);
    chartView1->setFixedSize(1920, 200);
    QThread thread1;
    TimerWorker worker1(series1,chart1);
    worker1.moveToThread(&thread1);
    QTimer timer1;
    QObject::connect(&timer1, &QTimer::timeout, &worker1, &TimerWorker::onTimeout);
    //////////////////////[]////////////////////////////////////////////////////
    /// \brief series2
    ///
    auto series2 = new QLineSeries;
    QColor color2(153, 76, 0);
    series2->setName("signal2");
    series2->setColor(color2);
    series2->setUseOpenGL(true);
    auto chart2 = new Chart;
    chart2->addSeries(series2);
    chart2->setTitle("SIGNAL2");
    chart2->setAnimationOptions(QChart::SeriesAnimations);
    chart2->setTheme(chart2->ChartThemeDark);
    auto axisX2 = new QValueAxis();
    axisX2->setTickCount(11);
    axisX2->setMin(0);
    axisX2->setMax(1440);
    axisX2->setTitleText("time2");
    //axisX2->hide();
    axisX2->setLineVisible(true);
    auto axisY2 = new QValueAxis();
    axisY2->setMin(0);
    axisY2->setMax(10);
    axisY2->setTickCount(11);
    axisY2->setTitleText("signals2");
    //axisY2->hide();
    chart2->setAxisX(axisX2, series2);
    chart2->setAxisY(axisY2, series2);
    auto chartView2 = new ChartView(chart2);
    chartView2->setRenderHint(QPainter::Antialiasing);
    chartView2->setFixedSize(1920, 200);
    QThread thread2;
    TimerWorker worker2(series2,chart2);
    worker2.moveToThread(&thread2);
    QTimer timer2;
    QObject::connect(&timer2, &QTimer::timeout, &worker2, &TimerWorker::onTimeout);
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////[]////////////////////////////////////////////////////
    /// \brief series3
    ///
    auto series3 = new QLineSeries;
    QColor color3(153, 76, 0);
    series3->setName("signal3");
    series3->setColor(color3);
    series3->setUseOpenGL(true);
    auto chart3 = new Chart;
    chart3->addSeries(series3);
    chart3->setTitle("SIGNAL3");
    chart3->setAnimationOptions(QChart::SeriesAnimations);
    chart3->setTheme(chart3->ChartThemeDark);
    auto axisX3 = new QValueAxis();
    axisX3->setTickCount(11);
    axisX3->setMin(0);
    axisX3->setMax(1440);
    axisX3->setTitleText("time3");
    //axisX3->hide();
    axisX3->setLineVisible(true);
    auto axisY3 = new QValueAxis();
    axisY3->setMin(0);
    axisY3->setMax(10);
    axisY3->setTickCount(11);
    axisY3->setTitleText("signals3");
    //axisY3->hide();
    chart3->setAxisX(axisX3, series3);
    chart3->setAxisY(axisY3, series3);
    auto chartView3 = new ChartView(chart3);
    chartView3->setRenderHint(QPainter::Antialiasing);
    chartView3->setFixedSize(1920, 200);
    QThread thread3;
    TimerWorker worker3(series3,chart3);
    worker3.moveToThread(&thread3);
    QTimer timer3;
    QObject::connect(&timer3, &QTimer::timeout, &worker3, &TimerWorker::onTimeout);
    //////////////////////[]////////////////////////////////////////////////////
    /// \brief series4
    ///
    auto series4 = new QLineSeries;
    QColor color4(153, 76, 0);
    series4->setName("signal4");
    series4->setColor(color4);
    series4->setUseOpenGL(true);
    auto chart4 = new Chart;
    chart4->addSeries(series4);
    chart4->setTitle("SIGNAL4");
    chart4->setAnimationOptions(QChart::SeriesAnimations);
    chart4->setTheme(chart4->ChartThemeDark);
    auto axisX4 = new QValueAxis();
    axisX4->setTickCount(11);
    axisX4->setMin(0);
    axisX4->setMax(1440);
    axisX4->setTitleText("time4");
    //axisX4->hide();
    axisX4->setLineVisible(true);
    auto axisY4 = new QValueAxis();
    axisY4->setMin(0);
    axisY4->setMax(10);
    axisY4->setTickCount(11);
    axisY4->setTitleText("signals4");
   // axisY4->hide();
    chart4->setAxisX(axisX4, series4);
    chart4->setAxisY(axisY4, series4);
    auto chartView4 = new ChartView(chart4);
    chartView4->setRenderHint(QPainter::Antialiasing);
    chartView4->setFixedSize(1920, 200);
    QThread thread4;
    TimerWorker worker4(series4,chart4);
    worker4.moveToThread(&thread4);
    QTimer timer4;
    QObject::connect(&timer4, &QTimer::timeout, &worker4, &TimerWorker::onTimeout);
    //////////////////////////////////////////////////////////////////////////////
    ///start [timer and thread]
    timer1.start(100);
    timer2.start(100);
    timer3.start(100);
    timer4.start(100);
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    //////////////////////////////////////////////////////////////////////////////
    MyMultiSignalChart window;
    QWidget *centralWidget = new QWidget();
    window.setCentralWidget(centralWidget);
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);
    ///////////////////////////////////////////////////////////////////////////////
    /// add [layout]
    layout->addWidget(chartView1);
    layout->addWidget(chartView2);
    layout->addWidget(chartView3);
    layout->addWidget(chartView4);
    layout->setDirection(QBoxLayout::Direction::TopToBottom);
    layout->setContentsMargins(0, 10, 10, 10); // 设置布局的边距
    window.setLayout(layout);
    window.resize(1920, 1080);
    window.grabGesture(Qt::PanGesture);
    window.grabGesture(Qt::PinchGesture);
    window.showMaximized();
    QObject::connect(&a, &QApplication::aboutToQuit, [&]() {
        ////////////////////////////////
        /// [stop]
        timer1.stop();
        timer2.stop();
        timer3.stop();
        timer4.stop();
        thread1.quit();
        thread2.quit();
        thread3.quit();
        thread4.quit();
        thread1.wait();
        thread2.wait();
        thread3.wait();
        thread4.wait();
    });
    return a.exec();
}
